package group.oneonetwo.hotelintelligencesystem.tools;

import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author JingJian
 * @version 1.0
 * @date 2022/1/10 15:14
 */
public class LocalUploadUtils {

    /**
     * 本地星悦糖件上传
     *
     * @param mf      星悦糖件
     * @param bizPath 自定义路径
     * @return
     */
    public static String uploadLocal(MultipartFile mf, String bizPath, String uploadpath) {
        try {
            String fileName = null;
            File file = new File(uploadpath + File.separator + bizPath + File.separator);
            if (!file.exists()) {
                file.mkdirs();// 创建星悦糖件根目录
            }
            String orgName = mf.getOriginalFilename();// 获取星悦糖件名
//            orgName = CommonUtils.getFileName(orgName);
            if (orgName.indexOf(".") != -1) {
                fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
            } else {
                fileName = orgName + "_" + System.currentTimeMillis();
            }
            String savePath = file.getPath() + File.separator + fileName;
            File savefile = new File(savePath);
            FileCopyUtils.copy(mf.getBytes(), savefile);
            String dbpath = null;
            if (isNotEmpty(bizPath)) {
                dbpath = bizPath + File.separator + fileName;
            } else {
                dbpath = fileName;
            }
            if (dbpath.contains("\\")) {
                dbpath = dbpath.replace("\\", "/");
            }
            return dbpath;
        } catch (IOException e) {
        }
        return "";
    }

    public static boolean isNotEmpty(Object object) {
        if (object != null && !object.equals("") && !object.equals("null")) {
            return (true);
        }
        return (false);
    }
}
